#
# Cookbook Name:: newrelic
# Recipe:: install_libertyagent
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

cuser = node[:chef][:user]
cgroup = node[:chef][:group]

#include_recipe "java7::install"


bash "Download newrelic java agent for liberty server" do
    user cuser
    cwd "/tmp/"
    creates "/tmp/downloadNewrelic.complete"
    code <<-EOH
      mkdir -p /tmp/newrelic/agent/java
      echo #{node[:common][:scp_repo]}
      scp #{node[:common][:scp_repo]}/#{node[:newrelic][:javadownloadpath]}/#{node[:newrelic][:javaagent]} /tmp/newrelic/agent/java && touch "/tmp/downloadNewrelic.complete"     
    EOH
end

bash "Install newrelic agent for liberty" do
  user "root"
  code <<-EOH
    unzip -o /tmp/newrelic/agent/java/#{node[:newrelic][:javaagent]} -d /opt/IBM
  EOH
end

# Update the newrelic configuration
template "/opt/IBM/newrelic/newrelic.yml" do
  source "newrelic.yml.erb"
  user "root"
  group "root"
  variables(
    :newrelic_java_agent_licensekey => node[:newrelic][:javalicensekey], 
    :applicatoin_name => node[:hostname],
    :newrelix_proxyhost => node[:newrelic][:proxyhost],
    :newrelix_proxyport => node[:newrelic][:proxyport])
  action :create
end

bash "Update jvm.options for newrelic" do
  user "root"
  code <<-EOH
   cd #{node[:liberty][:defaultServerDir]}
   sed -i '$a -javaagent:/opt/IBM/newrelic/newrelic.jar' jvm.options
  EOH
end

bash "Restart Liberty server" do
  user "root"
  code <<-EOH
    service libertyservice stop
    service libertyservice start
  EOH
end